# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time :
# @Author :
# @Email :
# @File :
# @Version: OpenTSN_planner_builder 1.0

from lib.tsn_builder_lib import host_to_net
import xml.etree.ElementTree as EleTree
from tsn_builder_struct.aux_tbl_structure import *


def link_aux_tbl(node,
                 aux_tbl,
                 reg_addr):
    # 找到该节点对应的aux表
    node_id = node['src_node']

    tbl_each_node = []

    for tbl in aux_tbl.tbl_set:
        if tbl.node_id == node_id:
            tbl_each_node = tbl.tbl_each_node
            break

    if not tbl_each_node:
        return []

    addr = reg_addr.aux_tbl[0]['addr']
    addr_str = host_to_net(addr)

    value_set = []
    for entry in tbl_each_node:
        value = entry.byte.entry1
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry2
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry3
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry4
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry5
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry6
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry7
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry8
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry9
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry10
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry11
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry12
        value_str = host_to_net(value)
        value_set.append(value_str)

    dic = [{'addr': addr_str,
            'value': value_set}]

    addr = reg_addr.aux_tbl[1]['addr']
    addr_str = host_to_net(addr)

    value_set = []
    for entry in tbl_each_node:
        value = entry.byte.entry13
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry14
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry15
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry16
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry17
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry18
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry19
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry20
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry21
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry22
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry23
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry24
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry25
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry26
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry27
        value_str = host_to_net(value)
        value_set.append(value_str)
        value = entry.byte.entry28
        value_str = host_to_net(value)
        value_set.append(value_str)

    dic.append({'addr': addr_str,
                'value': value_set})
    return dic


def _get_aux_tbl(test_aux_tbl_xml):
    test_aux_set = []
    root = EleTree.parse(test_aux_tbl_xml)
    node_ele_set = root.findall('node')
    for node_ele in node_ele_set:
        node = {}
        node_id = node_ele.findtext('node_id')
        node_id = int(node_id, 16)
        node.update({'node_id': int(node_id)})

        node.update({'entry': []})
        entry_ele = node_ele.find('entry')
        entry_ele = list(entry_ele)
        entry_dic = {}
        for ele in entry_ele:
            entry_dic.update({ele.tag: int(ele.text, 16)})
        node['entry'].append(entry_dic)
        test_aux_set.append(node)

    return test_aux_set


def _init_aux_tbl(test_aux_set):
    aux_tbl = AuxTbl()
    for node in test_aux_set:
        aux_tbl_each_node = AuxTblEachNode(node['node_id'])
        entry_each_node = node['entry']

        aux_entry = AuxEntry_union()
        aux_entry.bit.reserved1 = entry_each_node[0]['reserved1']
        aux_entry.bit.rx_flowid_p0 = entry_each_node[0]['rx_flowid_p0']
        aux_entry.bit.reserved2 = entry_each_node[0]['reserved2']
        aux_entry.bit.rx_flowid_p1 = entry_each_node[0]['rx_flowid_p1']
        aux_entry.bit.reserved3 = entry_each_node[0]['reserved3']
        aux_entry.bit.rx_flowid_p2 = entry_each_node[0]['rx_flowid_p2']
        aux_entry.bit.reserved4 = entry_each_node[0]['reserved4']
        aux_entry.bit.rx_flowid_p3 = entry_each_node[0]['rx_flowid_p3']

        aux_entry.bit.reserved5 = entry_each_node[0]['reserved5']
        aux_entry.bit.tx_flowid_p0 = entry_each_node[0]['tx_flowid_p0']
        aux_entry.bit.reserved6 = entry_each_node[0]['reserved6']
        aux_entry.bit.tx_flowid_p1 = entry_each_node[0]['tx_flowid_p1']
        aux_entry.bit.reserved7 = entry_each_node[0]['reserved7']
        aux_entry.bit.tx_flowid_p2 = entry_each_node[0]['tx_flowid_p2']
        aux_entry.bit.reserved8 = entry_each_node[0]['reserved8']
        aux_entry.bit.tx_flowid_p3 = entry_each_node[0]['tx_flowid_p3']

        aux_entry.bit.reserved9 = entry_each_node[0]['reserved9']
        aux_entry.bit.pto_select = entry_each_node[0]['pto_select']
        aux_entry.bit.tx_pulse = entry_each_node[0]['tx_pulse']
        aux_entry.bit.rx_pulse = entry_each_node[0]['rx_pulse']
        aux_entry.bit.reserved10 = entry_each_node[0]['reserved10']
        aux_entry.bit.dmac_1 = entry_each_node[0]['dmac_1']
        aux_entry.bit.dmac_2 = entry_each_node[0]['dmac_2']
        aux_entry.bit.reserved11 = entry_each_node[0]['reserved11']
        aux_entry.bit.mirror_mode = entry_each_node[0]['mirror_mode']

        aux_entry.bit.reserved12 = entry_each_node[0]['reserved12']
        aux_entry.bit.valid0 = entry_each_node[0]['valid0']
        aux_entry.bit.flow_id_0 = entry_each_node[0]['flow_id_0']
        aux_entry.bit.reserved13 = entry_each_node[0]['reserved13']
        aux_entry.bit.valid1 = entry_each_node[0]['valid1']
        aux_entry.bit.flow_id_1 = entry_each_node[0]['flow_id_1']
        aux_entry.bit.reserved14 = entry_each_node[0]['reserved14']
        aux_entry.bit.valid2 = entry_each_node[0]['valid2']
        aux_entry.bit.flow_id_2 = entry_each_node[0]['flow_id_2']
        aux_entry.bit.reserved15 = entry_each_node[0]['reserved15']
        aux_entry.bit.valid3 = entry_each_node[0]['valid3']
        aux_entry.bit.flow_id_3 = entry_each_node[0]['flow_id_3']
        aux_entry.bit.reserved16 = entry_each_node[0]['reserved16']
        aux_entry.bit.valid4 = entry_each_node[0]['valid4']
        aux_entry.bit.flow_id_4 = entry_each_node[0]['flow_id_4']
        aux_entry.bit.reserved17 = entry_each_node[0]['reserved17']
        aux_entry.bit.valid5 = entry_each_node[0]['valid5']
        aux_entry.bit.flow_id_5 = entry_each_node[0]['flow_id_5']
        aux_entry.bit.reserved18 = entry_each_node[0]['reserved18']
        aux_entry.bit.valid6 = entry_each_node[0]['valid6']
        aux_entry.bit.flow_id_6 = entry_each_node[0]['flow_id_6']
        aux_entry.bit.reserved19 = entry_each_node[0]['reserved19']
        aux_entry.bit.valid7 = entry_each_node[0]['valid7']
        aux_entry.bit.flow_id_7 = entry_each_node[0]['flow_id_7']
        aux_entry.bit.reserved20 = entry_each_node[0]['reserved20']
        aux_entry.bit.valid8 = entry_each_node[0]['valid8']
        aux_entry.bit.flow_id_8 = entry_each_node[0]['flow_id_8']
        aux_entry.bit.reserved21 = entry_each_node[0]['reserved21']
        aux_entry.bit.valid9 = entry_each_node[0]['valid9']
        aux_entry.bit.flow_id_9 = entry_each_node[0]['flow_id_9']
        aux_entry.bit.reserved22 = entry_each_node[0]['reserved22']
        aux_entry.bit.valid10 = entry_each_node[0]['valid10']
        aux_entry.bit.flow_id_10 = entry_each_node[0]['flow_id_10']
        aux_entry.bit.reserved23 = entry_each_node[0]['reserved23']
        aux_entry.bit.valid11 = entry_each_node[0]['valid11']
        aux_entry.bit.flow_id_11 = entry_each_node[0]['flow_id_11']
        aux_entry.bit.reserved24 = entry_each_node[0]['reserved24']
        aux_entry.bit.valid12 = entry_each_node[0]['valid12']
        aux_entry.bit.flow_id_12 = entry_each_node[0]['flow_id_12']
        aux_entry.bit.reserved25 = entry_each_node[0]['reserved25']
        aux_entry.bit.valid13 = entry_each_node[0]['valid13']
        aux_entry.bit.flow_id_13 = entry_each_node[0]['flow_id_13']
        aux_entry.bit.reserved26 = entry_each_node[0]['reserved26']
        aux_entry.bit.valid14 = entry_each_node[0]['valid14']
        aux_entry.bit.flow_id_14 = entry_each_node[0]['flow_id_14']
        aux_entry.bit.reserved27 = entry_each_node[0]['reserved27']
        aux_entry.bit.valid15 = entry_each_node[0]['valid15']
        aux_entry.bit.flow_id_15 = entry_each_node[0]['flow_id_15']

        aux_tbl_each_node.append_entry(aux_entry)
        aux_tbl.append_tbl(aux_tbl_each_node)
    return aux_tbl


def read_aux_tbl(test_aux_tbl_xml):
    test_aux_set = _get_aux_tbl(test_aux_tbl_xml)
    # print('DEBUG')
    # [print(entry) for entry in test_aux_set]
    test_aux_tbl = _init_aux_tbl(test_aux_set)

    return test_aux_tbl


def main():
    test_aux_tbl_xml = '../output_xml/tbl_xml/test_aux_tbl.xml'
    read_aux_tbl(test_aux_tbl_xml)


if __name__ == '__main__':
    main()
